package org.evolver.disser;

import javax.swing.*;
import org.apfloat.Apfloat;

public class DoubleParameter {
    class DoubleVerifier extends InputVerifier {
        public boolean verify(JComponent input) {
            boolean pass = true;

            JTextField tf = (JTextField) input;
            try {
                Double.valueOf(tf.getText());
            } catch (NumberFormatException e) {
                // If valueOf() above threw NumberFormatException
                // there is user error in the input data
                pass = false;
            }
            
            return pass;
        }
    }
    private String     myLabel;
    private String     myUnit;
    private Apfloat    myValue;
    private JTextField myField;

    public DoubleParameter(String label, String unit, Double defaultValue) {
        myLabel = label;
        myUnit = unit;
        myValue = new Apfloat(defaultValue);
    }

    public void addToPanel(JPanel p) {
        p.add(new JLabel(myLabel), "gap 10");
        myField = new JTextField(myValue.toString(true));
        myField.setInputVerifier(new DoubleVerifier());
        p.add(myField, "w 75!");
        p.add(new JLabel(myUnit), "wrap");
    }

    public Double getValue() {
        myValue = new Apfloat(Double.valueOf(myField.getText()));
        return myValue.doubleValue();
    }
}
